package top.lshaci.learning.jsoup;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

/**
 * ImgFetch
 *
 * @author liangxu
 */
public class ImgFetch {
    static String path = "D:\\fuligirl";

    public static void main(String[] args) throws Exception {
//        String url = "https://fuligirl.top/models/104";
//        fetch(url);
        String modelUrl = "https://fuligirl.top/models";
        models(modelUrl);
    }

    private static void models(String modelUrl) throws Exception {
        Document document = Jsoup.connect(modelUrl).get();
        Elements relative = document.select("div.relative");
        for (Element element : relative) {
            String modelName = element.selectFirst("img[title]").attr("title");
            String modelPath = path + "\\" + modelName;
            mkdir(modelPath);
            String href = element.selectFirst("a").attr("href");
            fetch(modelPath, href);
        }
        Element next = document.selectFirst("a[rel='next']");
        if (next == null) {
            return;
        }
        models(next.attr("href"));
    }

    private static void fetch(String modelPath, String url) throws Exception {
        Document document = Jsoup.connect(url).get();
        Elements relative = document.select("div.relative");
        for (Element element : relative) {
            String title = element.selectFirst("img[title]").attr("title");
            String dirPath = modelPath + "\\" + title;
            mkdir(dirPath);
            String href = element.selectFirst("a").attr("href");
            detailPage(dirPath, href);
        }
        Element next = document.selectFirst("a[rel='next']");
        if (next == null) {
            return;
        }
        fetch(modelPath, next.attr("href"));
    }

    private static void detailPage(String dirPath, String href) throws Exception {
        Document detail = Jsoup.connect(href).get();
        Elements imgs = detail.select("img.block.my-1");
        for (Element img : imgs) {
            String imgSrc = img.attr("src");
            download(imgSrc, dirPath, img.attr("title") + ".jpg");
        }
        Element next = detail.selectFirst("a[rel='next']");
        if (next == null) {
            return;
        }
        detailPage(dirPath, next.attr("href"));
    }

    private static void download(String imgUrl, String dirPath, String fileName) throws Exception {
        System.err.println("保存图片: " + fileName);
        FileOutputStream out = new FileOutputStream(dirPath + "\\" + fileName);
        HttpUtil.download(imgUrl, out, true);
        TimeUnit.SECONDS.sleep(RandomUtil.randomInt(1, 5));
    }

    private static void mkdir(String dirPath) {
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }
}
